package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.text.TextUtils;
import android.widget.RatingBar;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.tencent.qcloud.ugckit.utils.ToastUtil;
import com.whmnx.base.bean.AppreaiseBean;
import com.whmnx.base.bean.ImgResultBean;
import com.whmnx.base.bean.OrderDetailBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.FileUtil;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.databinding.ActivityRequest2Binding;
import com.whmnx.my.ui.activity.AppraiseActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/10/28 0028 17:29
 * version: 1.0
 */
public class AppreaiseModel extends ToolbarViewModel{
    public ObservableField<String> Product_Name = new ObservableField<>("");
    public ObservableField<String> Product_Price = new ObservableField<>("");
    public ObservableField<String> Product_Num = new ObservableField<>("");
    public ObservableField<String> Product_SPEC = new ObservableField<>("");
   // public ObservableField<String> Price = new ObservableField<>("");
    public ObservableField<String> etInput = new ObservableField<>("");


    public AppraiseActivity mActivity;
    public ActivityRequest2Binding mBinding;
    public  String orderId;

    private String photosUrl = "";

    private List<String> data;

    private ArrayList<String> list_path = new ArrayList<>();
    private float rating2;
    private String ping_order_id;
    private List<AppreaiseBean.CommentBean.CommentImageModelBean> commentImageModel;
    private String product_id;

    public AppreaiseModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar(AppraiseActivity requestActivity, ActivityRequest2Binding binding, String orderId) {
        this.mActivity = requestActivity;
        this.orderId = orderId;
        this.mBinding = binding;
        setTitleText("发表评价");
        getOrderdetail();
        initView();
    }

    private void initView() {
        mBinding.ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                rating2 = rating;
            }
        });
    }

    private void getOrderdetail() {
        ping_order_id = SPUtils.getInstance().getString("ping_order_id");

        product_id = mActivity.getIntent().getStringExtra("Product_Id");
        Map<String, Object> params = new HashMap<>();
        if (TextUtils.isEmpty(ping_order_id)){
            params.put("OrderId",orderId);
        }else {
            params.put("OrderId", ping_order_id);
        }

        HttpClient.get(mActivity, Constants.ORDERDETAIL, params,new CallBack<OrderDetailBean>() {
            @Override
            public void onSuccess(OrderDetailBean result) {
                observableList.clear();
                if (TextUtils.isEmpty(product_id)){
                    for (OrderDetailBean.ProductListBean productListBean : result.getProductList()) {
                        observableList.add(new OrderItemViewModel4(AppreaiseModel.this,productListBean));
                    }
                }else {
                    for (OrderDetailBean.ProductListBean productListBean2 : result.getProductList()) {
                           if (productListBean2.getProduct_Id().equals(product_id)){
                               observableList.add(new OrderItemViewModel4(AppreaiseModel.this,productListBean2));
                           }
                    }
                }
            }
        });
    }

    //给RecyclerView添加ObservableList
    public ObservableList<OrderItemViewModel4> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<OrderItemViewModel4> itemBinding = ItemBinding.of(BR.viewModel, R.layout.grid_order4);



    //提交
    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            data = mBinding.snplMomentAddPhotos.getData();
            if (TextUtils.isEmpty(etInput.get())){
                ToastUtil.toastShortMessage("");
                return;
            }

            if (data.size()==0){
                ToastUtil.toastShortMessage("请先添加图片");
                return;
            }
            list_path.clear();
            List<File> files = new ArrayList<>();
            for (String datum : data) {
                File file = new File(FileUtil.compressImage(datum));
                files.add(file);
            }

            HttpClient.postImg(mActivity, Constants.UPLOADHEADIMG, files, new CallBack<List<ImgResultBean>>() {
                @Override
                public void onSuccess(List<ImgResultBean> baseBean) {
                    for (ImgResultBean imgResultBean : baseBean) {
                        list_path.add(imgResultBean.shortPath);
                    }
                    getUpdate();
                }
            });
        }
    });

    private void getUpdate() {
        AppreaiseBean requestBean  =  new AppreaiseBean();
        requestBean.setOrderNo(SPUtils.getInstance().getString("ping_order_No"));


        requestBean.setUserInfo_Id(SPUtils.getInstance().getString(Constants.USERID));

        List<AppreaiseBean.CommentBean> commentBeans = new ArrayList<>();
        AppreaiseBean.CommentBean commentBean =  new AppreaiseBean.CommentBean();


        for (String s : list_path) {
            String s1;
            if (s.contains("http")){
                s1 = s.split(Constants.HOST)[1];
            }else {
                s1 = s;
            }
            photosUrl += s1 + ",";
        }

        List<AppreaiseBean.CommentBean.CommentImageModelBean> commentImageModel = new ArrayList<>();
        AppreaiseBean.CommentBean.CommentImageModelBean  commentImageModelBean = new AppreaiseBean.CommentBean.CommentImageModelBean();
        commentImageModelBean.setImage(photosUrl);

        commentImageModel.add(commentImageModelBean);
        commentBean.setCommentImageModel(commentImageModel);
        commentBean.setComment_Praise((int)rating2);
        commentBean.setComment_Content(etInput.get());

        String  product_id2 = SPUtils.getInstance().getString("Product_Id2");
        if (!TextUtils.isEmpty(product_id2)){
            commentBean.setProduct_Id(product_id2);
        }else {
            commentBean.setProduct_Id(product_id);
        }


        commentBeans.add(commentBean);

        requestBean.setComment(commentBeans);


        Map<String, Object> params = new HashMap<>();
        params.put("",requestBean);
        HttpClient.post(mActivity, Constants.ADDCOMMENT, params,new CallBack<OrderDetailBean>() {
            @Override
            public void onSuccess(OrderDetailBean result) {
                RxBus.getDefault().post(EventConstants.REQUEST);
                finish();
            }
        });
    }
}
